package controler;

import java.io.*;
import java.util.*;

import person.*;

public class FileControler {
	private static FileControler fileControler = new FileControler();

	private FileControler() {

	}

	public static FileControler getFileControler() {
		return fileControler;
	}

	public void save(PersonRecorder personRecorder) {
		Vector<PersonRecorder> savedPersonRecorderArray = getSaveFile();
//		 Vector<PersonRecorder> personRecorders = new Vector<>();

		savedPersonRecorderArray.add(personRecorder);
		File file = new File("save");
		ObjectOutputStream outputStream = null;

		if (file != null) {
			try {
				outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
				outputStream.writeObject(savedPersonRecorderArray);
			} catch (Exception e) {
				// TODO: handle exception
			} finally {
				try {
					if (outputStream != null)
						outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void saveExerciseInfo(PersonRecorder personRecorder) {
		Vector<PersonRecorder> savedPersonRecorderArray = getSaveFile();
//		 Vector<PersonRecorder> personRecorders = new Vector<>();

		int indexOfPersonRecorder = savedPersonRecorderArray.indexOf(personRecorder);
		savedPersonRecorderArray.set(indexOfPersonRecorder, personRecorder);
		File file = new File("save");
		ObjectOutputStream outputStream = null;

		if (file != null) {
			try {
				outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
				outputStream.writeObject(savedPersonRecorderArray);
			} catch (Exception e) {
				// TODO: handle exception
			} finally {
				try {
					if (outputStream != null)
						outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void saveExerciseInfo(PersonRecorder personRecorder, int indexOfSavedPersonRecorder) {
		Vector<PersonRecorder> savedPersonRecorderArray = getSaveFile();
//		 Vector<PersonRecorder> personRecorders = new Vector<>();

		savedPersonRecorderArray.set(indexOfSavedPersonRecorder, personRecorder);
		File file = new File("save");
		ObjectOutputStream outputStream = null;

		if (file != null) {
			try {
				outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
				outputStream.writeObject(savedPersonRecorderArray);
			} catch (Exception e) {
				// TODO: handle exception
			} finally {
				try {
					if (outputStream != null)
						outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	public Vector<PersonRecorder> getSaveFile() {
		Vector<PersonRecorder> personRecorders = null;
//		Vector<PersonRecorder> personRecorders = new Vector<>();
		File file = new File("save");
		ObjectInputStream objectInputStream = null;

		if (file != null) {
			try {
				objectInputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
				Object obj = objectInputStream.readObject();
				personRecorders = (Vector<PersonRecorder>) obj;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (objectInputStream != null) {
					try {
						objectInputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

		return personRecorders;
	}
	
	public void deleteData(int index) {
		Vector<PersonRecorder> savedData = getSaveFile();
		
		savedData.remove(index);
		
		File file = new File("save");
		ObjectOutputStream outputStream = null;

		if (file != null) {
			try {
				outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
				outputStream.writeObject(savedData);
			} catch (Exception e) {
				// TODO: handle exception
			} finally {
				try {
					if (outputStream != null)
						outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	

}
